﻿using Android.Content;
using Android.Widget;
using DataProcess;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;

namespace BlackBoxForU.Feature
{
    public class SettingUtil
    {
        public static string Sign
        {
            get;
            private set;
        }
        public static string SettingDir
        {
            get => Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/BlackBox";
        }
        public static string SettingFile
        {
            get => "/Reward.txt";
        }
        public static void Init(Context context)
        {
            if (!Directory.Exists(SettingDir))
            {
                Directory.CreateDirectory(SettingDir);
            }
            if (!File.Exists(SettingDir + SettingFile))
            {
                MakeDefaultSetting(SettingDir + SettingFile);
            }
            try
            {
                ChoiseWords[] choises = ReadChoiseFromFile(SettingDir + SettingFile);
                Sign = SettingSign(choises);
                LuckDraw.Append(choises);
            }
            catch
            {
                Toast.MakeText(context, "读取配置文件错误，设置预处理文件", ToastLength.Short).Show();
                File.Delete(SettingDir + SettingFile);
                Init(context);
            }
        }
        [Serializable]
        class SerialbleChoise
        {
            public string Content
            {
                get;
                set;
            }
            public int Number
            {
                get;
                set;
            }
            public static SerialbleChoise Parse(ChoiseWords choise)
            {
                return new SerialbleChoise() { Content = choise.Content, Number = choise.Number };
            }
        }

        public static string SettingSign(ChoiseWords[] choises)
        {
            string text = "";
            using (MemoryStream stream = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                SerialbleChoise[] serialbles = new SerialbleChoise[choises.Length];
                for (int i = 0; i < serialbles.Length; i++)
                {
                    serialbles[i] = SerialbleChoise.Parse(choises[i]);
                }

                formatter.Serialize(stream, serialbles);
                byte[] arr = DataProcess.Encryption.HMACMaker.HMAC(Encoding.UTF8.GetBytes("Ryq&Tbs"), stream.ToArray(), DataProcess.Encryption.HMACMaker.Level.SHA1);

                for (int i = 7; i < 10; i++)
                {
                    text += DataProcess.Mathematics.NumberCase.ExchangeFromTen(arr[i], 16, 2);
                }
            }
            return text;
        }
        public static ChoiseWords[] ReadChoiseFromFile(string file)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(file);
            List<ChoiseWords> ls = new List<ChoiseWords>();
            XmlNodeList list = xml.DocumentElement.ChildNodes;
            for (int i = 0; i < list.Count; i++)
            {
                try
                {
                    ChoiseWords choise = new ChoiseWords();
                    var item = list.Item(i);
                    string content = item.Attributes.GetNamedItem("Content").Value;
                    int op = int.Parse(item.Attributes.GetNamedItem("Opportunity").Value);
                    choise.Content = content;
                    choise.Number = op;
                    ls.Add(choise);
                }
                catch
                {

                }

            }
            int nop = 55;
            int.TryParse(xml.DocumentElement.GetAttribute("NothingOp"), out nop);
            int moreop = 25;
            int.TryParse(xml.DocumentElement.GetAttribute("MoreOp"), out moreop);
            ls.Add(new ChoiseWords() { Content = "再来一次", Number = moreop });
            ls.Add(new ChoiseWords() { Content = "未中奖", Number = nop });
            return ls.ToArray();
        }
        public static void MakeDefaultSetting(string file)
        {
            XmlDocument xml = new XmlDocument();
            XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
            xml.AppendChild(dec);
            XmlElement XMLroot = xml.CreateElement("Reward");
            XMLroot.SetAttribute("NothingOp", "45");
            XMLroot.SetAttribute("MoreOp", "25");
            XmlElement element = xml.CreateElement("Choise");
            element.SetAttribute("Content", "大奖");
            element.SetAttribute("Opportunity", "1");
            XmlElement element1 = xml.CreateElement("Choise");
            element1.SetAttribute("Content", "小奖");
            element1.SetAttribute("Opportunity", "10");
            XmlElement element2 = xml.CreateElement("Choise");
            element2.SetAttribute("Content", "鼓励奖");
            element2.SetAttribute("Opportunity", "19");
            XMLroot.AppendChild(element);
            XMLroot.AppendChild(element1);
            XMLroot.AppendChild(element2);
            xml.AppendChild(XMLroot);
            xml.Save(file);
        }
    }
}